﻿using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.EntityFramework;


namespace CarRental.DDL {
    public class DbAccess : LinqToEntitiesDomainService<car_rental_dbEntities> {

        car_rental_dbEntities __carRentalEntities = new car_rental_dbEntities();
        public DbAccess() { }

        #region order_List
        public IQueryable<order_list> GetOrderLst() {
            return ObjectContext.order_list;
        }

        public void InsertOrderLst(order_list pOrderLst) {
            if (pOrderLst.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pOrderLst, EntityState.Added);
            } else {
                __carRentalEntities.order_list.AddObject(pOrderLst);
            }
            __carRentalEntities.SaveChanges();
        }

        public void UpdateOrderLst(order_list pOrderLst) {

            var orderlist = (from x in __carRentalEntities.order_list where x.id == pOrderLst.id select x).First();
            orderlist = pOrderLst;

            __carRentalEntities.order_list.ApplyCurrentValues(orderlist);
            __carRentalEntities.SaveChanges();
        }
        #endregion

        #region order_Detail
        public IQueryable<order_detail> GetOrderDtl() {
            return ObjectContext.order_detail;
        }

        public void InsertOrderDtl(order_detail pOrderDtl) {
            if (pOrderDtl.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pOrderDtl, EntityState.Added);
            } else {
                __carRentalEntities.order_detail.AddObject(pOrderDtl);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }

        public void UpdateOrderDtl(order_detail pOrderDtl) {

            var orderdetail = (from x in __carRentalEntities.order_detail where x.id == pOrderDtl.id select x).First();
            orderdetail = pOrderDtl;

            __carRentalEntities.order_detail.ApplyCurrentValues(orderdetail);
            __carRentalEntities.SaveChanges();

            __carRentalEntities.Dispose();
        }
        #endregion

        #region user
        public IQueryable<user> GetUser() {
            return ObjectContext.user;
        }

        public void InsertUser(user pUser) {
            if (pUser.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pUser, EntityState.Added);
            } else {
                __carRentalEntities.user.AddObject(pUser);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }

        public void UpdateUser(user pUser) {

            var User = (from x in __carRentalEntities.user where x.id == pUser.id select x).First();
            User = pUser;

            __carRentalEntities.user.ApplyCurrentValues(User);
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region car
        public IQueryable<car> GetCar() {
            return ObjectContext.car;
        }

        public void InsertCar(car pCar) {
            if (pCar.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pCar, EntityState.Added);
            } else {
                __carRentalEntities.car.AddObject(pCar);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }

        public void UpdateCar(car pCar) {
            var Car = (from x in __carRentalEntities.car where x.id == pCar.id select x).First();
            Car = pCar;
            __carRentalEntities.car.ApplyCurrentValues(Car);
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }

        public void DeleteCar(car pCar) {
            var car = (from x in __carRentalEntities.car where x.id == pCar.id select x).First();

            if (pCar.EntityState == EntityState.Detached) {
                __carRentalEntities.car.Attach(car);
            }
            __carRentalEntities.car.DeleteObject(car);

            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region vwcar
        public IQueryable<vwcar> GetVwCar() {
            return ObjectContext.vwcar;
        }
        #endregion

        #region CarType
        public IQueryable<car_type> GetCarType() {
            return ObjectContext.car_type;
        }

        public void InsertCar(car_type pCarType) {
            if (pCarType.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pCarType, EntityState.Added);
            } else {
                __carRentalEntities.car_type.AddObject(pCarType);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region shop
        public IQueryable<shop> GetShop() {
            return ObjectContext.shop;
        }

        public void InsertShopByTest() {
            shop _shop = new shop();
            _shop.shopcode = "A1";
            _shop.shopname = "CarShop 1";
            _shop.address = "Hong Kong";

            if (_shop.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(_shop, EntityState.Added);
            } else {
                __carRentalEntities.shop.AddObject(_shop);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();

            //using (__carRentalEntities = new CarRentalEntities()) {
            //    if (_shop.EntityState != EntityState.Detached)
            //    {
            //        __carRentalEntities.ObjectStateManager.ChangeObjectState(_shop, EntityState.Added);
            //    }
            //    else
            //    {
            //        __carRentalEntities.shop.AddObject(_shop);
            //    }
            //    __carRentalEntities.SaveChanges();
            //}
        }
        #endregion

        #region Depot
        public IQueryable<depot> GetDepot() {
            return ObjectContext.depot;
        }
        #endregion

        #region Drivetrains
        public IQueryable<drivetrain> GetDrivetrains() {
            return ObjectContext.drivetrain;
        }
        #endregion

        #region spqGetCarAllowToRent
        public List<vwcar> GetspqGetCarAllowToRent(DateTime pFromDate, DateTime pToDate, string pShopCode) {
            return ObjectContext.spqGetCarAllowToRent(pFromDate, pToDate, pShopCode).ToList<vwcar>();
        }
        #endregion

        #region SeqNo
        public string GetSeqNo(string pTableName) {
            string _seqNoStr = "";
            string _prefix = "";
            Int64 _No = 0;
            seqno _seqno = new seqno();

            _seqno = __carRentalEntities.seqno.Where(w => w.tableName == pTableName).FirstOrDefault();
            if (_seqno != null) {
                _prefix = _seqno.prefix ?? "";
                _No = _seqno.No ?? 0;
            }
            
            _seqNoStr = _prefix + _No;

            _seqno.No += 1;
            UpdateSeqNo(_seqno);

            return _seqNoStr;
        }

        public void UpdateSeqNo(seqno currentSeqno) {
            __carRentalEntities.seqno.ApplyCurrentValues(currentSeqno);
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion


    }
}